package jzOffer;

public class Num68of2 {
    TreeNode lca;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null) return null;
        find(root,p,q);
        return lca;
    }
    private boolean find(TreeNode root, TreeNode p, TreeNode q){
        if(root==null) return false;
        int left=find(root.left,p,q)?1:0;
        int right=find(root.right,p,q)?1:0;
        int mid=(root==p || root==q)?1:0;

        if(left+right+mid==2){
            lca=root;
            return true;
        }

        return (left+right+mid)>0;
    }
}
